<template>
	<view>
		<topDropdown :isSxShow="true" :isPaiXu="true" :dropdown1="dropdown1" :options1="options1" :optionsSx="sxList"
			:optionsPx="optionsPx" :isDuoXuan="true" searchLabel1="公告名称" searchPh1="请输入公告名称"
			@searchBoxEmitFun="searchBoxEmitFun" @dropDownFun1="dropDownFun1" @optionSxFun="optionSxFun"
			@optionPxFun="optionPxFun"></topDropdown>
		<scroll-view scroll-y="true" :style="{height: scrollHeight}" @scrolltolower="selectKehuFun" refresher-enabled
			:refresher-threshold="200" :refresher-triggered="triggered" refresher-background="gray"
			@refresherrefresh="onRefresh" @refresherrestore="onRestore">
			<view v-if="list.length > 0">
				<view v-for="(item, index) in list" :key="index">
					<notifyCard :item="item" :isSelect="isSelect" :index="index" @cxGetDataFun="cxGetDataFun"></notifyCard>
				</view>
				<getMore :isMore="isMore" nullMsg="已加载全部~"></getMore>
				<view class="h200"></view>
			</view>
			<dataNull v-else src="/static/img/dataNull.png" title="暂无相关线索哦~"></dataNull>
		</scroll-view>
		<u-action-sheet :list="sheetList" v-model="moreShow"></u-action-sheet>
	</view>
</template>

<script>
	let that = this;
	import { apiSysNotifyUser } from '@/api/admin.js'
	import {
		tyDropDown,
		cjDropDownData,
		notifySxDropDownData,
		dealDatePxData
	} from '@/utils/dropdown.js'
	import {
		getDayFun,
		formatDate,
		getDayByNumFun
	} from '@/utils/date.js'

	import dataNull from '@/components/dataNull/dataNull.vue'
	import getMore from '@/components/getMore/getMore.vue'
	import lianxiRow from '@/components/lianxiRow/lianxiRow.vue'
	import topDropdown from '@/components/topDropdown/topDropdown.vue'
	import notifyCard from '@/components/card/sysNotifyUserCard.vue'

	export default {
		components: {
			dataNull,
			getMore,
			topDropdown,
			lianxiRow,
			notifyCard
		},
		data() {
			return {
				triggered: false,
				dropdown1: '全部类型',
				options1: tyDropDown, //数据查看权限
				optionsPx: dealDatePxData, //时间排序
				sxList: [],
				list: [],
				moreShow: false,
				sheetList: [{
						text: '添加跟进'
					},
					{
						text: '添加标签'
					}
				],
				isMore: true,
				pageNum: 1,
				scrollHeight: '667px',
				pageType: '',
				sortObj: {},
				optionsReq: {}, // 第一个下拉框请求参数
				sortReq: {
					orderField: 'create_time',
					orderDirection:'desc'
				}, // 第二个排序请求参数
				dateReq: [], // 日期相关请求参数
				sxReq: {}, // 多条件筛选请求参数
				matchObj: {},
				searchValue: '',
				isSelect: false,
				depId: undefined
			}
		},
		onLoad(e) {
			that = this;
			that.sxList =notifySxDropDownData;
			
			that.options1=[
				{label:'全部类型',value:0,listtype:''},
				{label:'已读公告',value:1,listtype:'readed'},
				{label:'未读公告',value:2,listtype:'unreaded'},
			];
			
			
			console.log('页面加载：');

			if (e.type) {
				that.isSelect = true;
			}

			that.pageType = e.type ? e.type : '';
			uni.getSystemInfo({
				success(res) {
					that.scrollHeight = res.windowHeight - 40 + 'px';
				}
			})
			
			
			
			that.selectKehuFun(); //加载选择列表
			uni.$on('deleteKhFun', that.deleteKhFun)
			uni.$on('updateListByIndex', that.updateListByIndex)
			uni.$on('addItemInListFun', that.addItemInListFun)
			uni.$on('cxGetDataFun', that.cxGetDataFun)
			uni.$on('clueListChangeFun', that.cxGetDataFun)
			uni.$on('updateNotifyStatus', that.updateNotifyStatus)
		},
		onBackPress() {
			uni.$off('deleteKhFun', that.deleteKhFun)
			uni.$off('updateListByIndex', that.updateListByIndex)
			uni.$off('addItemInListFun', that.addItemInListFun)
			uni.$off('cxGetDataFun', that.cxGetDataFun)
			uni.$off('clueListChangeFun', that.cxGetDataFun)
			uni.$off('updateNotifyStatus', that.updateNotifyStatus)
		},
		methods: {
			// 查询客户
			selectKehuFun: function() {
				if (!that.isMore) {
					return
				}
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				let matchObj = {
					deal_status: 0,//未处理的
					deal_user_id: uni.$userInfo.id,
					keywords: that.searchValue
				}
				
				Object.assign(matchObj, that.matchObj,that.optionsReq, that.sxReq);
				
				let reqObj = {
					matchObj: matchObj,
					pageNum: that.pageNum,
					sortObj: that.sortReq,
				}
				let reqData = {
					action: 'get_list',
					params: reqObj
				}
				apiSysNotifyUser(reqData)
					.then(res => {
						this.triggered = false;
						let data = res.data.data;
						console.log(data)
						if (that.pageNum == 1) {
							that.list = [];
						}
						if (data.length == 10) {
							that.pageNum += 1;
							that.isMore = true;
						} else {
							that.isMore = false;
						}
						that.list = that.list.concat(data);
						setTimeout(() => {
							//延时关闭  加载中的 loading框
							uni.hideLoading()
						}, 888);

					})
			},
			// 下拉框回调函数
			dropDownFun1: function(e) {
				let optionsReq = {};
				
				console.log(e.listtype);
				
				if (e.label == '全部类型') {
					optionsReq.listtype = '';
				} else if (e.label == '已读公告') {
					optionsReq.listtype = 'readed';
				} else if (e.label == '未读公告') {
					optionsReq.listtype = 'unreaded';
				}
				
				that.optionsReq = optionsReq;
				
				console.log('第一个下拉参数：')
				console.log(optionsReq.listtype)
				
				that.cxGetDataFun();
			},
			// 筛选框回调函数
			optionSxFun: function(e) {
				that.sxReq = e;
				that.cxGetDataFun();
			},
			// 排序框回调函数
			optionPxFun: function(e) {
				that.sortReq = e;
				that.cxGetDataFun();
			},
			// 搜索框回调函数
			searchBoxEmitFun: function(e) {
				that.searchValue = e;
				that.cxGetDataFun();
			},
			// 刷新数据
			cxGetDataFun: function() {
				that.pageNum = 1;
				that.isMore = true;
				that.list = [];
				that.selectKehuFun();
			},
			// 下拉刷新
			onRefresh: function() {
				if (this.triggered) return;
				this.triggered = true;
				this.cxGetDataFun();
			},
			// 刷新还原
			onRestore: function() {
				this.triggered = false;
			},
			// 删除客户
			deleteKhFun: function(e) {
				let index = e.index;
				that.list.splice(index, 1);
			},
			// 更新列表
			updateListByIndex: function(e) {
				let index = e.index;
				let obj = e.obj;
				Object.assign(that.list[index], obj);
				console.log('更新列表成功')
			},
			// 添加一个元素到列表
			addItemInListFun: function(e) {
				if (that.list.length == 0) {
					that.pageNum = 1;
					that.selectKehuFun();
					return;
				}
				that.list.unshift(e.item);
			},
			// 更新公告状态
			updateNotifyStatus: function(e) {
				let index = e.index;
				that.list[index].deal_status = e.deal_status;
				that.list[index].deal_time = e.deal_time;
			}
		}
	}
</script>

<style>
	.h200 {
		height: 200rpx;
	}
</style>